#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 6e3 + 10, INF = 0x3f3f3f3f;

int n;
int a[N], f[N][2], in[N];
vector<int> g[N];

void dfs(int x)
{
    f[x][0] = 0, f[x][1] = a[x];
    for (auto y : g[x]) {
        dfs(y);
        f[x][0] += max(f[y][1], f[y][0]);
        f[x][1] += f[y][0];
    }
}

void solve()
{
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> a[i];
    for (int i = 1;i < n;i++) {
        int x, y;cin >> x >> y;
        g[y].push_back(x);
        in[x]++;
    }
    int root = -1;
    for (int i = 1;i <= n;i++) if (in[i] == 0) { root = i;break; }

    dfs(root);
    cout << max({ f[root][0], f[root][1], 0 });
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}